package com.wushijia.highconcurrency;

import java.util.concurrent.locks.ReentrantLock;

/**
 * description:公平锁
 *
 * @author yang
 * @date 2018/12/21 14:44
 */
public class FairLock implements Runnable {

  private static ReentrantLock fairLock = new ReentrantLock(true);

  @Override
  public void run() {
    while (true) {
      try {
        fairLock.lock();
        System.out.println(Thread.currentThread().getName() + "获得锁");
      } finally {
        fairLock.unlock();
      }
    }
  }

  public static void main(String[] args) {
    FairLock fairLock1 = new FairLock();
    Thread t1 = new Thread(fairLock1, "thread1");
    Thread t2 = new Thread(fairLock1, "thread2");
    t1.start();
    t2.start();
  }
}
